/**
 * 
 */
package com.deep.test.nio;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;

/**
 * @author HDP23
 *
 */
public class NioClient {
	//信道选择器  
    private Selector selector;  
  
    // 与服务器通信的信道  
    SocketChannel socketChannel;  
  
    // 要连接的服务器Ip地址  
    private String hostIp;  
  
    // 要连接的远程服务器在监听的端口  
    private int hostListenningPort;  
    public NioClient(String hostIp,int port) throws IOException{
    	this.hostIp=hostIp;
    	this.hostListenningPort=port;
    	init();
    }
	private void init() throws IOException {
		// 打开监听信道并设置为非阻塞模式  
        socketChannel = SocketChannel.open(new InetSocketAddress(hostIp, hostListenningPort));  
        socketChannel.configureBlocking(false);  
  
        // 打开并注册选择器到信道  
        selector = Selector.open();  
        socketChannel.register(selector, SelectionKey.OP_READ);  
  
        // 启动读取线程  
        new TcpClientReadThread(selector);  
		
	}
	/** 
     * 发送字符串到服务器 
     *  
     * @param message 
     * @throws IOException 
     */  
    public void sendMsg(String message) throws IOException {  
        ByteBuffer writeBuffer = ByteBuffer.wrap(message.getBytes("utf-8"));  
        int r = socketChannel.write(writeBuffer);  
        System.out.println("write return:" + r);  
        //socketChannel.  
    }  
  
    public static void main(String[] args) throws IOException {  
    	NioClient client = new NioClient(InetAddress.getLocalHost().getHostAddress(), 7210);  
            client.sendMsg("Nio hello word");  
            /*try { 
                Thread.sleep(20); 
            } catch (InterruptedException e) { 
                // TODO Auto-generated catch block 
                e.printStackTrace(); 
            }*/  
          
    }  
}
